<template>
	<view class="order-detail-container">
		<scroll-view class="detail-content" scroll-y>
			<!-- 订单状态 -->
			<view class="status-section">
				<view class="status-title" v-if="orderDetail.status === '0'">
					<text class="status-text">待取货，等待到店取货</text>
				</view>
				<view class="status-title" v-if="orderDetail.status === '1'">
					<text class="status-text">待配送，等待商家配送</text>
				</view>
				<view class="status-title" v-if="orderDetail.status === '2'">
					<text class="status-text">待取货，商家配送中</text>
				</view>
				<view class="status-title" v-if="orderDetail.status === '3'">
					<text class="status-text">已完成，等待到店取货</text>
				</view>
				<view class="status-title" v-if="orderDetail.status === '4'">
					<text class="status-text">等待付款</text>
				</view>
				<view class="status-title" v-if="orderDetail.status === '5'">
					<text class="status-text">已取消，您的订单已自助取消</text>
				</view>

			</view>

			<!-- 收货地址 - 仅显示在非取消状态且需要配送的订单 -->
			<view class="address-section" v-if="orderDetail.type === '1' && orderDetail.status !== '5'">
				<view class="address-info">
					<view class="recipient-info">
						<text class="recipient-name">{{orderDetail.recipient}}</text>
						<text class="recipient-phone">{{orderDetail.phone}}</text>
					</view>
					<view class="address-detail">
						<text class="address-text">{{orderDetail.area}}
							{{orderDetail.detailedAddress}}</text>
					</view>
				</view>
			</view>

			<!-- 订单进度 -->
			<view class="progress-section">
				<view class="progress-item">
					<view class="progress-dot active"></view>
					<view class="progress-content">
						<text class="progress-title">提交订单</text>
						<text class="progress-time">{{orderDetail.createTime}}</text>
					</view>
				</view>
				<view class="progress-item" v-if="orderDetail.payTime">
					<view class="progress-dot active"></view>
					<view class="progress-content">
						<text class="progress-title">付款完成</text>
						<text class="progress-time">{{orderDetail.payTime}}</text>
					</view>
				</view>
				<view class="progress-item" v-if="orderDetail.cancelTime">
					<view class="progress-dot active"></view>
					<view class="progress-content">
						<text class="progress-title">取消</text>
						<text class="progress-time">{{formatTime(orderDetail.cancelTime)}}</text>
					</view>
				</view>
			</view>

			<!-- 商品信息 -->
			<view class="goods-section">
				<view class="section-header">
					<text class="section-title">商品信息</text>
				</view>
				<view class="goods-list">
					<view class="goods-item" v-for="goods in orderDetail.goodsList" :key="goods.id">
						<view class="goods-image">
							<image :src="goods.photo" mode="aspectFill" class="image" v-if="goods.photo"></image>
							<view class="image-placeholder" v-else>
								<text>暂无图片</text>
							</view>
						</view>
						<view class="goods-info">
							<view class="goods-name">
								<text class="name-text">{{goods.name}}</text>
							</view>
							<view class="goods-spec" v-if="goods.classifyName">
								<text class="spec-text">{{goods.classifyName}}</text>
							</view>
							<!-- <view class="goods-tags">
								<text class="tag">支持七天无理由退货</text>
							</view> -->
							<view class="goods-price">
								<text class="price-text">¥{{goods.price}}</text>
								<text class="quantity">x{{goods.buyNum || 1}}</text>
							</view>
						</view>
					</view>
				</view>
			</view>

			<!-- 联系客服 -->
			<!-- <view class="service-section">
				<view class="service-item">
					<text class="service-text">联系客服</text>
					<u-icon name="arrow-right" size="16" color="#999"></u-icon>
				</view>
			</view> -->

			<!-- 进群领福利 -->
			<!-- <view class="promotion-section">
				<view class="promotion-content">
					<text class="promotion-title">进群领福利</text>
					<text class="promotion-desc">超值促销信息抢先知 · 贴心当当管家服务点击进群 >></text>
				</view>
			</view> -->

			<!-- 订单信息 -->
			<view class="order-info-section">
				<view class="info-item">
					<text class="info-label">订单编号：</text>
					<text class="info-value">{{orderDetail.id}}</text>
				</view>
				<view class="info-item">
					<text class="info-label">下单时间：</text>
					<text class="info-value">{{orderDetail.createTime}}</text>
				</view>

				<!-- 正常订单的额外信息 -->
				<template v-if="orderDetail.status !== '5'">
					<view class="info-item" v-if="orderDetail.paymentMethod">
						<text class="info-label">付款方式：</text>
						<text class="info-value">{{orderDetail.paymentMethod}}</text>
					</view>
					<view class="info-item" v-if="orderDetail.payTime">
						<text class="info-label">支付时间：</text>
						<text class="info-value">{{orderDetail.payTime}}</text>
					</view>
					<view class="info-item" v-if="orderDetail.deliveryMethod">
						<text class="info-label">配送方式：</text>
						<text class="info-value">{{orderDetail.deliveryMethod}}</text>
					</view>
				</template>
			</view>

			<!-- 发票信息 -->
			<!-- <view class="invoice-section" v-if="orderDetail.status !== '5'">
				<view class="info-item">
					<text class="info-label">发票信息：</text>
					<text class="info-value">{{orderDetail.invoiceInfo || '不开发票'}}</text>
				</view>
			</view> -->

			<!-- 价格明细 -->
			<view class="price-section">
				<view class="price-item">
					<text class="price-label">商品金额总计：</text>
					<text class="price-value">¥{{orderDetail.totalAmount || '0.00'}}</text>
				</view>
				<view class="price-item">
					<text class="price-label">运费：</text>
					<text class="price-value">¥{{orderDetail.freight || '0.00'}}</text>
				</view>
				<view class="price-item total">
					<text class="price-label">实付款：</text>
					<text
						class="price-value total-amount">¥{{orderDetail.actualAmount || orderDetail.totalAmount || '0.00'}}</text>
				</view>
			</view>

			<!-- 底部安全区域 -->
			<view class="safe-area"></view>
		</scroll-view>

		<!-- 底部操作按钮 -->
		<!-- <view class="action-buttons">
			<view class="button-group">
				<u-button type="primary" plain text="查看退款"
					customStyle="width: 160rpx; height: 70rpx; border-color: #666; color: #666;"
					@click="handleViewRefund"></u-button>
				<u-button type="primary" text="再次购买"
					customStyle="width: 160rpx; height: 70rpx; background: #ff463c; border: none;"
					@click="handleRebuy"></u-button>
			</view>
		</view> -->
	</view>
</template>

<script>
	import {
		getGoodsOrderDetail
	} from "@/api/order.js";
	import config from '@/config';
	export default {
		data() {
			return {
				orderId: '',
				orderDetail: {
					goodsList: []
				}
			};
		},
		onLoad(options) {
			if (options.orderId) {
				this.orderId = options.orderId;
				this.getOrderDetail();
			}
		},
		methods: {
			async getOrderDetail() {
				try {
					const res = await getGoodsOrderDetail(this.orderId);
					if (res.code === 200) {
						this.orderDetail = res.data;
						this.orderDetail.goodsList.forEach(item => {
							if (item.photo != null) {
								item.photo = config.baseUrl + item.photo;
							}
						})
					} else {
						uni.showToast({
							title: '获取订单详情失败',
							icon: 'none'
						});
					}
				} catch (error) {
					console.error('获取订单详情错误:', error);
					uni.showToast({
						title: '网络错误',
						icon: 'none'
					});
				}
			},

			handleBack() {
				uni.navigateBack();
			},

			getStatusText(status) {
				const statusMap = {
					"0": "待取书",
					"1": "待配送",
					"2": "配送中",
					"3": "已完成",
					"4": "待付款",
					"5": "已取消",
				};
				return statusMap[status] || "未知状态";
			},

			formatTime(time) {
				if (!time) return '';
				// 如果是时间戳，转换为日期格式
				if (typeof time === 'number') {
					const date = new Date(time);
					return date.toLocaleString('zh-CN', {
						year: 'numeric',
						month: '2-digit',
						day: '2-digit',
						hour: '2-digit',
						minute: '2-digit',
						second: '2-digit',
						hour12: false
					}).replace(/\//g, '-');
				}
				return time;
			},

			handleViewRefund() {
				uni.showToast({
					title: '查看退款',
					icon: 'none'
				});
			},

			handleRebuy() {
				uni.showToast({
					title: '再次购买',
					icon: 'none'
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.order-detail-container {
		height: 100vh;
		background-color: #f6f6f6;
		display: flex;
		flex-direction: column;
	}

	/* 导航头部 */
	.nav-header {
		height: 88rpx;
		background-color: #ffffff;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 30rpx;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.nav-back {
		width: 60rpx;
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: flex-start;
	}

	.nav-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333333;
	}

	.nav-placeholder {
		width: 60rpx;
	}

	/* 内容区域 */
	.detail-content {
		flex: 1;
	}

	/* 状态区域 */
	.status-section {
		background-color: #ffffff;
		padding: 30rpx;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.status-title {
		display: flex;
		align-items: center;
	}

	.status-text {
		font-size: 32rpx;
		font-weight: bold;
		color: #333333;
	}

	/* 地址区域 */
	.address-section {
		background-color: #ffffff;
		padding: 30rpx;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.address-info {
		display: flex;
		flex-direction: column;
	}

	.recipient-info {
		display: flex;
		align-items: center;
		margin-bottom: 15rpx;
	}

	.recipient-name {
		font-size: 32rpx;
		font-weight: bold;
		color: #333333;
		margin-right: 30rpx;
	}

	.recipient-phone {
		font-size: 28rpx;
		color: #666666;
	}

	.address-detail {
		margin-bottom: 10rpx;
	}

	.address-text {
		font-size: 28rpx;
		color: #666666;
		line-height: 1.5;
	}

	/* 进度区域 */
	.progress-section {
		background-color: #ffffff;
		padding: 30rpx;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.progress-item {
		display: flex;
		align-items: flex-start;
		margin-bottom: 40rpx;
	}

	.progress-item:last-child {
		margin-bottom: 0;
	}

	.progress-dot {
		width: 20rpx;
		height: 20rpx;
		border-radius: 50%;
		background-color: #ddd;
		margin-right: 30rpx;
		margin-top: 8rpx;
	}

	.progress-dot.active {
		background-color: #ff463c;
	}

	.progress-content {
		flex: 1;
		display: flex;
		flex-direction: column;
	}

	.progress-title {
		font-size: 30rpx;
		color: #333333;
		margin-bottom: 8rpx;
	}

	.progress-time {
		font-size: 26rpx;
		color: #999999;
	}

	/* 商品区域 */
	.goods-section {
		background-color: #ffffff;
		margin: 20rpx 0;
	}

	.section-header {
		padding: 30rpx 30rpx 20rpx;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.section-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333333;
	}

	.goods-list {
		padding: 0 30rpx;
	}

	.goods-item {
		display: flex;
		padding: 30rpx 0;
		border-bottom: 1rpx solid #f8f8f8;
	}

	.goods-item:last-child {
		border-bottom: none;
	}

	.goods-image {
		width: 160rpx;
		height: 160rpx;
		border-radius: 12rpx;
		overflow: hidden;
		background-color: #f5f5f5;
	}

	.image {
		width: 100%;
		height: 100%;
	}

	.image-placeholder {
		width: 100%;
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #e0e0e0;
	}

	.image-placeholder text {
		font-size: 24rpx;
		color: #999999;
	}

	.goods-info {
		flex: 1;
		margin-left: 24rpx;
		display: flex;
		flex-direction: column;
	}

	.goods-name {
		margin-bottom: 12rpx;
	}

	.name-text {
		font-size: 30rpx;
		font-weight: 500;
		color: #333333;
		line-height: 1.4;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
	}

	.goods-spec {
		margin-bottom: 12rpx;
	}

	.spec-text {
		font-size: 26rpx;
		color: #999999;
	}

	.goods-tags {
		margin-bottom: 20rpx;
	}

	.tag {
		font-size: 24rpx;
		color: #1989fa;
		background-color: #ecf5ff;
		padding: 6rpx 12rpx;
		border-radius: 6rpx;
	}

	.goods-price {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: auto;
	}

	.price-text {
		font-size: 32rpx;
		font-weight: bold;
		color: #ff463c;
	}

	.quantity {
		font-size: 28rpx;
		color: #999999;
	}

	/* 服务区域 */
	.service-section {
		background-color: #ffffff;
		margin: 20rpx 0;
		padding: 0 30rpx;
	}

	.service-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 30rpx 0;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.service-text {
		font-size: 30rpx;
		color: #333333;
	}

	/* 促销区域 */
	.promotion-section {
		background-color: #ffffff;
		margin: 20rpx 0;
		padding: 30rpx;
	}

	.promotion-content {
		display: flex;
		flex-direction: column;
		align-items: center;
		background: linear-gradient(135deg, #ff6b6b, #ff8e8e);
		padding: 40rpx 30rpx;
		border-radius: 16rpx;
	}

	.promotion-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #ffffff;
		margin-bottom: 15rpx;
	}

	.promotion-desc {
		font-size: 26rpx;
		color: #ffffff;
		opacity: 0.9;
	}

	/* 订单信息区域 */
	.order-info-section {
		background-color: #ffffff;
		margin: 20rpx 0;
		padding: 30rpx;
	}

	.info-item {
		display: flex;
		justify-content: space-between;
		margin-bottom: 20rpx;
	}

	.info-item:last-child {
		margin-bottom: 0;
	}

	.info-label {
		font-size: 28rpx;
		color: #666666;
	}

	.info-value {
		font-size: 28rpx;
		color: #333333;
	}

	/* 发票区域 */
	.invoice-section {
		background-color: #ffffff;
		margin: 20rpx 0;
		padding: 30rpx;
	}

	/* 价格区域 */
	.price-section {
		background-color: #ffffff;
		margin: 20rpx 0;
		padding: 30rpx;
	}

	.price-item {
		display: flex;
		justify-content: space-between;
		margin-bottom: 20rpx;
	}

	.price-item:last-child {
		margin-bottom: 0;
	}

	.price-item.total {
		border-top: 1rpx solid #f0f0f0;
		padding-top: 20rpx;
		margin-top: 20rpx;
	}

	.price-label {
		font-size: 28rpx;
		color: #666666;
	}

	.price-value {
		font-size: 28rpx;
		color: #333333;
	}

	.total-amount {
		font-size: 32rpx;
		font-weight: bold;
		color: #ff463c;
	}

	/* 安全区域 */
	.safe-area {
		height: 120rpx;
	}

	/* 底部操作按钮 */
	.action-buttons {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background: #ffffff;
		padding: 20rpx 30rpx;
		border-top: 1rpx solid #f0f0f0;
	}

	.button-group {
		display: flex;
		justify-content: flex-end;
		gap: 20rpx;
	}
</style>